-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Update substring match for substitutions #148061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
6e1a961 to
3a0a76b
Compare
This comment has been minimized.
This comment has been minimized.
|
Well, I guess we can't modify this I guess we should somehow modify this function rust/compiler/rustc_resolve/src/imports.rs Line 736 in ab92564
This function was pointed by error[E0432]: unresolved import `sync`
--> /home/gh-Kivooeo/test_/src/main.rs:2:5
|
2 | use sync;
| ^^^^ no `sync` in the root
|
= note: -Ztrack-diagnostics: created at compiler/rustc_resolve/src/imports.rs:768:24
help: consider importing this module instead
|
2 | use std::sync;
| +++++This one generates a final suggestion, but I don't really sure how this tide with So, what is my idea about, in the place where we generating suggestion (I guess this is function above, but I'm not 100% sure) we need to check what we got:
so if we got first we are fine with that, if we got second then we should somehow move I will be honest that I have no idea how to implement that in that function, I maybe will took another look but later, so there is just some of my thoughts on the core idea how this should work |
diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs
index 17cd466f96b..2ed4e281adb 100644
--- a/compiler/rustc_errors/src/lib.rs
+++ b/compiler/rustc_errors/src/lib.rs
@@ -306,6 +306,18 @@ fn replaces_meaningful_content(&self, sm: &SourceMap) -> bool {
/// `BB` is. Return the length of the prefix, the "trimmed" suggestion, and the length
/// of the suffix.
fn as_substr<'a>(original: &'a str, suggestion: &'a str) -> Option<(usize, &'a str, usize)> {
+ if suggestion.contains("::")
+ && ["core", "std"].iter().any(|&prefix| suggestion.starts_with(prefix))
+ && suggestion.ends_with(original)
+ && let Some(c1) = suggestion.chars().nth(0)
+ && let Some(c2) = original.chars().nth(0)
+ && c1 == c2
+ {
+ let prefix_len = suggestion.len() - original.len();
+ let prefix = &suggestion[..prefix_len];
+ return Some((0, prefix, original.len()));
+ }
+
let common_prefix = original
.chars()
.zip(suggestion.chars())Btw, just decided to check will this work or not... and sadly it is, all tests passed, and it gave correct suggestion But... I'd consider this as a last resort if we can't come up with a better solution |
|
Yeah, I was just trying to generalize that solution, as that does not catch third party crates. But yeah, it looks like the solution is that way. |
|
I guess it's possible to remove It's a bit late for me atm (3 am) so I would able to test it without this check later today Or if you want to test it yourself, you can copy this into your rustc fork and run But I'd be happier to see an implementation of my idea above, I also will try to investigate this direction more later on today |
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
This last potential fix I pushed has only one test failing. I missed that for some reason locally, I think I'm missing something for proper testing. I think this is an acceptable compromise (as is the case when there's a nested diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs
index 9cd34a8747f..5e4bd514af8 100644
--- a/compiler/rustc_errors/src/lib.rs
+++ b/compiler/rustc_errors/src/lib.rs
@@ -312,7 +312,10 @@ fn as_substr<'a>(original: &'a str, suggestion: &'a str) -> Option<(usize, &'a s
&& suggestion.len() > original.len()
{
let prefix = &suggestion[..suggestion.len() - original.len()];
- if prefix.ends_with("::") && suggestion.chars().next() == original.chars().next() {
+ if prefix.ends_with("::")
+ && suggestion.chars().next() == original.chars().next()
+ && suggestion.trim_suffix("::") != original
+ {
return Some((0, prefix, original.len()));
}
} |
Fixed
as_substrfunction to check for matching prefixes too. This fixes this issue wheretd::sis matched instead ofstd::. This same behaviour can be seen withtimeandtokio::timeand similar cases where the error import starts with the same prefix as the suggestion.After the changes: